package CAD.shujuku;


import java.beans.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.Scanner;

public class Stuff {
    private static PreparedStatement preparedStatement;
    private static ResultSet resultSet;
    private static int i;
    private static Scanner sc=new Scanner(System.in);
    public static  void selection() {
        try {
            String sql = "select * from dept";
            PreparedStatement rs = JdbcMysqlTest.jdbc().prepareStatement(sql);
            resultSet = rs.executeQuery();
            while (resultSet.next()) {
                int deptno = resultSet.getInt("deptno");
                String dname = resultSet.getString("dname");
                String loc = resultSet.getString("loc");
                System.out.println(deptno + ":" + dname + ":" + loc + ":");
                //resultSet.close();
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void add() throws SQLException, ClassNotFoundException {
            System.out.println("请输入要添加的部门号：");
            int deptno =sc.nextInt();
            System.out.println("请输入要添加的部门名称：");
            String dname = sc.next();
            System.out.println("请输入要添加工作地点：");
            String loc = sc.next();
            String sql = "insert into dept() values ('"+ deptno+"','"+dname+"','"+loc+"')";
            PreparedStatement rs = JdbcMysqlTest.jdbc().prepareStatement(sql);
            i= rs.executeUpdate();
            selection();
    }
    public static void delete() throws SQLException, ClassNotFoundException {


            System.out.println("请输入要删除部门的部门号：");
            int deptno =sc.nextInt();
            String sql = "delete from dept where deptno='"+deptno+"'";
            PreparedStatement rs = JdbcMysqlTest.jdbc().prepareStatement(sql);
            i= rs.executeUpdate();
            selection();
    }
    public static  void alter() throws SQLException, ClassNotFoundException {
            String sql;
            System.out.println("请输入要修改的部门号：");
            int deptno =sc.nextInt();
            System.out.println("请选择修改的内容：1.部门号2.部门名称3.工作地点");
            int num =sc.nextInt();
            switch (num){
                case 1:
                    System.out.println("请输入新的部门号：");
                    int newdeptno = sc.nextInt();
                    sql = "update dept set deptno='"+newdeptno+"' where deptno='"+deptno+"'";
                    preparedStatement = JdbcMysqlTest.jdbc().prepareStatement(sql);
                    i=preparedStatement.executeUpdate();
                    break;
                case 2:
                    System.out.println("请输入新的部门名称：");
                    int newdname = sc.nextInt();
                    sql = "update dept set dname='"+newdname+"' where deptno='"+deptno+"'";
                    preparedStatement = JdbcMysqlTest.jdbc().prepareStatement(sql);
                    i=preparedStatement.executeUpdate();
                    break;
                case 3:
                    System.out.println("请输入新的工作地点：");
                    int newloc = sc.nextInt();
                    sql = "update dept set dname='"+newloc+"' where deptno='"+deptno+"'";
                    preparedStatement = JdbcMysqlTest.jdbc().prepareStatement(sql);
                    i=preparedStatement.executeUpdate();
                    break;
            }
            if(i==1){
                System.out.println("修改成功！！");
            }else{
                System.out.println("修改失败！！");
            }
            selection();
    }
public static  void denglu() throws SQLException, ClassNotFoundException {
    System.out.println("********部门管理系统********");
    System.out.println("**************************");
    System.out.println("请输入您的账号");
    String user = sc.next();
    System.out.println("请输入您的密码");
    String password = sc.next();
    login(user,password);
    if(login(user,password)){
        System.out.println("恭喜你！登录成功！");
        System.out.println("********部门管理系统********");
        System.out.println("*******1.查讯全部信息*******");
        System.out.println("*******2.添加部门信息*******");
        System.out.println("*******3.修改部门信息*******");
        System.out.println("*******4.删除部门信息*******");
        System.out.println("*******0.退出系统**********");
        System.out.print("请选择你要进行的操作：");
        int num = sc.nextInt();
        switch(num){
            case 1:
                selection();

            case 2:
                add();

            case 3:
                alter();

            case 4:
                delete();
            case 0:
                break;
            default:
                System.out.println("输入错误,已退出系统！！！");
                break;
        }




    }else{
        System.out.println("您输入的用户名或密码错误,将退出系统！！");
    }

}
    private static boolean login(String user, String password) throws SQLException, ClassNotFoundException {
        boolean a=true;
            String sql = "select * from buser where user='"+user+"' and password='"+password+"' ";
        PreparedStatement rs = JdbcMysqlTest.jdbc().prepareStatement(sql);
        resultSet = rs.executeQuery();

            if(resultSet.next()){
                a=true;
            }else{
                a=false;
            }
        return a;
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
denglu();
    }
}
